/*
 * Ejercicio realizado por Francisco Gonzalez-Armijo Riádigos
 * para la prueba de seleccion de http://www.nomasystems.es
 */

package es.kuassivi.android.cd.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import es.kuassivi.android.cd.R;
import es.kuassivi.android.cd.beans.Address;

/**
 * Adapter CompanyAddressAdapter
 * Adaptador de vista para direcciones postales, el cual implementa la informacion 
 * de la direccion postal de una compañia en los campos text de la vista
 * 
 * @author kuassivi <a href="mailto:kuassivi@gmail.com">kuassivi@gmail.com</a>
 * @since 0.1
 */
public class CompanyAddressAdapter extends BaseAdapter 
{
	
	/**
	 * @var LayoutInflater
	 */
	private LayoutInflater mInflater;
	
	/**
	 * @var List&lt;Address>
	 */
	private List<Address> mAddress;
	
	
	/**
	 * Inicia el adaptador para la vista
	 * 
	 * @param context Context
	 * @param address List&lt;Address> Listado de objetos Address
	 * @see Address
	 * @see ArrayList
	 */
	public CompanyAddressAdapter(Context context, List<Address> address) 
	{
		//mInflater = LayoutInflater.from(context);
		
		super();
		
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);	
		
		mAddress = address;
	}

	@Override
	public int getCount() 
	{
		if (mAddress == null)
			return 0;
		else
			return mAddress.size();
	}

	@Override
	public Object getItem(int position) 
	{
		return mAddress.get(position);
	}

	@Override
	public long getItemId(int position) 
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		ViewHolder holder;
		
		if (convertView == null) {
			
			convertView = mInflater.inflate(R.layout.company_address_view, null);
			
			holder = new ViewHolder();
			
			holder.txt_company_address = (TextView) convertView.findViewById(R.id.txt_company_address);
			
			convertView.setTag(holder);
			
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.txt_company_address.setText(mAddress.get(position).getAddress());
		
		return convertView;
	}
	
	/**
	 * Creamos un objeto contenedor, que será un soporte de vista 
	 * indentico al objeto de vista donde queremos cargar los datos
	 */
	private class ViewHolder {
		TextView txt_company_address;
	}
}
